from sqlalchemy import Column, Integer, String, Date, DateTime

from core.base.base_model import BaseModel
from core.utils.time_utils import DateTimeUtils

from views.Task.model import Task


class Tomato(BaseModel):
    __tablename__ = 'tomato'  # 表名

    employee_id = Column(Integer)
    task_id = Column(Integer)
    group_id = Column(Integer,info=dict(object="TomatoGroup"))
    begin_time = Column(DateTime, doc='开始时间')
    end_time = Column(DateTime, doc='结束时间')
    status = Column(Integer,doc='状态') # 0未开始 1进行中 2 已完成

    @classmethod
    def handle_tomato(cls, info):
        task_id, employee_id = info.get("task_id"), info.get("employee_id")
        res, tomato = None, None
        if task_id:
            tomato = cls.db().query(cls.id).filter(
                cls.employee_id == employee_id,
                cls.task_id == task_id,
                cls.status == 1
            ).first()

        if tomato:
            res = cls.update_data(tomato.id, info)
        else:
            res = cls.create_data(info)

        return res

    @classmethod
    def get_tomatos(cls, date=None):
        _query = cls.db().query(cls, Task).join(Task, cls.task_id == Task.id)
        if date:
            _begin_time, _end_time = DateTimeUtils.get_date_start_end(date)
            _query = _query.filter(
                cls.begin_time >= _begin_time,
                cls.end_time <= _end_time
            )

        return [_i.json for _i in _query.all()]









